package com.library.web.back_record;

import com.library.dao.ReturnDAO;
import com.library.model.ReturnRecord;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@WebServlet("/back_book")
public class BackBookServlet extends HttpServlet {

    private final ReturnDAO returnDAO = new ReturnDAO();
    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");

        if (action == null || "list".equals(action)) {
            String username = request.getParameter("username");
            String bookTitle = request.getParameter("bookTitle");
            String hasFine = request.getParameter("hasFine"); // YES/NO
            List<ReturnRecord> returnRecords = returnDAO.findByCondition(username, bookTitle, hasFine);
            request.setAttribute("returnRecords", returnRecords);
            request.getRequestDispatcher("back_book/backBook.jsp").forward(request, response);
        } else if ("delete".equals(action)) {
            try {
                int id = Integer.parseInt(request.getParameter("id"));
                returnDAO.delete(id);
            } catch (NumberFormatException e) {
                e.printStackTrace();
            }
            response.sendRedirect("back_book?action=list");
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");

        if ("add".equals(action)) {
            try {
                ReturnRecord returnRecord = new ReturnRecord();
                returnRecord.setUsername(request.getParameter("username"));
                returnRecord.setPhone(request.getParameter("phone"));
                returnRecord.setBookTitle(request.getParameter("bookTitle"));
                returnRecord.setBorrowDate(sdf.parse(request.getParameter("borrowDate")));
                returnRecord.setExpectReturnDate(sdf.parse(request.getParameter("expectReturnDate")));
                returnRecord.setReturnDate(sdf.parse(request.getParameter("returnDate")));
                returnRecord.setOverdueDays(Integer.parseInt(request.getParameter("overdueDays")));
                returnRecord.setFineAmount(Double.parseDouble(request.getParameter("fineAmount")));
                returnDAO.add(returnRecord);
            } catch (ParseException | NumberFormatException e) {
                e.printStackTrace();
            }
            response.sendRedirect("back_book?action=list");
        }
        // 2. 编辑归还记录
        else if ("update".equals(action)) {
            try {
                ReturnRecord returnRecord = new ReturnRecord();
                returnRecord.setId(Integer.parseInt(request.getParameter("id")));
                returnRecord.setUsername(request.getParameter("username"));
                returnRecord.setPhone(request.getParameter("phone"));
                returnRecord.setBookTitle(request.getParameter("bookTitle"));
                returnRecord.setBorrowDate(sdf.parse(request.getParameter("borrowDate")));
                returnRecord.setExpectReturnDate(sdf.parse(request.getParameter("expectReturnDate")));
                returnRecord.setReturnDate(sdf.parse(request.getParameter("returnDate")));
                returnRecord.setOverdueDays(Integer.parseInt(request.getParameter("overdueDays")));
                returnRecord.setFineAmount(Double.parseDouble(request.getParameter("fineAmount")));
                returnDAO.update(returnRecord);
            } catch (ParseException | NumberFormatException e) {
                e.printStackTrace();
            }
            response.sendRedirect("back_book?action=list");
        }
    }
}
